/*
* Libresweeper By Loveaabb <https://gitee.com/loveaabb>
* -- main.cpp --
* 许可证：MIT
*/

#include <cstdio>
#include <cstdlib>
#include <ctime>

#include "msweeper.h"

#define VERSION "v0.5"

#define GB 13
#define RB 14

//using namespace std;

int main()
{
	time_t t;
	char ch, choice;
//	int dbg = 0;
	Stat revres = SAFESTEPPED;
//	bool started = false;
	bool over = false, finished = false, showmine = false;
	int width = 8, height = 8, minecount = 10;

	srand(time(&t));

	printf("------------- LibreSweeper %s -------------\n", VERSION);
	printf("- 简单: 9*9 10个雷\n");
	printf("- 普通: 16*16 40个雷\n");
	printf("- 困难: 16*30 99个雷\n");
	printf("请选择难度 (简单/普通/困难/自定义)[e/i/h/c]: ");
	scanf("%c", &choice);
	fflush(stdin);

	switch (choice)
	{
		case 'e':
			width = 9;
			height = 9;
			minecount = 10;
			break;
		case 'i':
			width = 16;
			height = 16;
			minecount = 40;
			break;
		case 'h':
			width = 30;
			height = 16;
			minecount = 99;
			break;
		case 'c':
			printf("\n请输入行数 列数 雷数 (空格分隔): ");
			scanf("%d%d%d", &height, &width, &minecount);

			if (height < 1) height = 1;
			if (width < 1) width = 1;
			if (minecount > height * width - 1) minecount = height * width - 1;
			break;
	}

	initscr();
	start_color();
	noecho();

	init_pair(GB, COLOR_GREEN, COLOR_BLACK);
	init_pair(RB, COLOR_RED, COLOR_BLACK);


	Map gamemap(width, height, minecount);
	gamemap.generate_map();

	while(true)
	{
		if (gamemap.isFinished())
		{
			finished = true;
			move(height + 1, 0);
			printw("you won!");
			move(height + 2, 0);
			printw("Press 'r' to restart");
		}


		if (over)
		{
			showmine = true;
			move(height + 1, 0);
			printw("BOOM!");
			move(height + 2, 0);
			printw("Press 'r' to restart");
		}
		else
		{
			showmine = false;
		}


		gamemap.print(0, 0, showmine);

		if (gamemap.get_flagged() < gamemap.get_mineNum())
		{
			printw("%d/%d          ", gamemap.get_flagged(), gamemap.get_mineNum());
		}
		else if (gamemap.get_flagged() == gamemap.get_mineNum())
		{
			attron(COLOR_PAIR(GB));
			printw("%d/%d          ", gamemap.get_flagged(), gamemap.get_mineNum());
			attroff(COLOR_PAIR(GB));
		}
		else
		{
			attron(COLOR_PAIR(RB));
			printw("%d/%d          ", gamemap.get_flagged(), gamemap.get_mineNum());
			attroff(COLOR_PAIR(RB));
		}

//		dbg++;
		ch = getch();


		if (!over && !finished)
		{
			if (ch == 'w' || ch == 'a' || ch == 's' || ch == 'd')
			{
				gamemap.cursor_move(ch);
			}
			else if (ch == ' ')
			{
				revres = gamemap.reveal(gamemap.getcx(), gamemap.getcy());

				if (revres == MINESTEPPED)
				{
					;
					over = true;
				}
			}
			else if (ch == 'f')
			{
				gamemap.toggle_flag(gamemap.getcx(), gamemap.getcy());
			}

		}
		if (ch == 'r')
		{
			gamemap.generate_map();
			finished = false;
			over = false;

			move(height + 1, 0);
			printw("                                  ");
			move(height + 2, 0);
			printw("                                  ");

		}

	}

    return 0;
}
